﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DataAccessor.Tools
{
    /// <summary>
    /// DatabaseCollection变化事件
    /// </summary>
    public delegate void DatabaseCollectionChangedEventHandler();
    public partial class FormDatabase : Form
    {
        #region properties
        /// <summary>
        /// the holistic object.
        /// </summary>
        private readonly Holistic _holistic;
        #endregion

        #region events
        public event DatabaseCollectionChangedEventHandler DatabaseCollectionChanged;
        private void OnDatabaseCollectionChanged()
        {
            if (DatabaseCollectionChanged != null)
            {
                DatabaseCollectionChanged();
            }
        }
        public event DatabaseChangedEventHandler DatabaseSettingChanged;
        private void OnDatabaseSettingChanged(UCDatabase sender)
        {
            if (DatabaseSettingChanged != null)
            {
                DatabaseSettingChanged(sender);
            }
        }
        #endregion

        /// <summary>
        /// create an instance of FormDatabaseSetting
        /// </summary>
        /// <param name="holistic">the holistic object</param>
        public FormDatabase(Holistic holistic)
        {
            this._holistic = holistic;
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            LoadSettingForm();
        }

        private void pictureCreate_Click(object sender, EventArgs e)
        {
            var splitter = BuildSplitter();
            var settingForm = BuildDatabaseSetting();
            settingForm.Tag = splitter;
            List<Control> range = new List<Control> { splitter, settingForm };
            panelSettingsContainer.Controls.AddRange(range.ToArray());

            var database = settingForm.Database;
            List<HolisticDatabase> databaseList = null;
            if (_holistic.Database != null)
            {
                databaseList = _holistic.Database.ToList();
            }
            else
            {
                databaseList = new List<HolisticDatabase>();
            }
            databaseList.Add(database);
            _holistic.Database = databaseList.ToArray();
            OnDatabaseCollectionChanged();
        }

        private void LoadSettingForm()
        {
            if (_holistic.Database == null)
            {
                return;
            }

            List<Control> range = new List<Control>();
            foreach (var database in _holistic.Database)
            {
                var splitter = BuildSplitter();
                var settingForm = BuildDatabaseSetting(database);
                settingForm.Tag = splitter;
                settingForm.MiniMode = true;

                range.Add(splitter);
                range.Add(settingForm);
            }
            panelSettingsContainer.Controls.AddRange(range.ToArray());
        }

        private UCDatabase BuildDatabaseSetting(HolisticDatabase database = null)
        {
            UCDatabase setting = new UCDatabase()
            {
                Dock = DockStyle.Top,
                Database = database
            };
            setting.DatabaseChanged += new DatabaseChangedEventHandler(setting_DatabaseSettingChanged);
            setting.ContextMenuStrip = this.contextMenu;
            setting.Show();
            return setting;
        }

        void setting_DatabaseSettingChanged(UCDatabase sender)
        {
            this.OnDatabaseSettingChanged(sender);
        }

        private Control BuildSplitter()
        {
            Panel splitter = new Panel()
            {
                Dock = DockStyle.Top,
                Height = 5
            };
            return splitter;
        }

        private void cmiDel_Click(object sender, EventArgs e)
        {
            var settingForm = contextMenu.SourceControl.Parent as UCDatabase;
            var splitter = settingForm.Tag as Panel;

            var holisticList = _holistic.Database.ToList();
            holisticList.Remove(settingForm.Database);

            _holistic.Database = holisticList.ToArray();
            OnDatabaseCollectionChanged();

            settingForm.Parent.Controls.Remove(splitter);
            settingForm.Parent.Controls.Remove(settingForm);

            splitter.Dispose();
            settingForm.Dispose();
        }
    }
}
